﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.player
{
    partial class SangoPlayerHandler
    {
        public player_actionbuy player_actionbuy()
        {
            string userCode = SangoManager.Authentication();

            player_actionbuy response = ReflectionManager.CreatePojo<player_actionbuy>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Activity activity = UsrActivityDao.getHistory(userCode);

            if (!PlayerFormular.canBuyActionPoint(profile, activity))
            {
                response.Message = "无法购买军令";
                return response;
            }

            long goldprice = PlayerFormular.getActionPointGoldPrice(activity);

            if (!UsrProfileDao.tryDecreaseGoldValue(profile, goldprice))
            {
                response.Message = "金币不足";
                return response;
            }

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                activity.Battlebuycount = (activity.Battlebuycount + 1);
                activity.Freebattlecount = (activity.Freebattlecount + 1);

                command.Update(activity);

                command.Update(profile);

                command.Commit();
            });

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.activity = UsrProfileFacade.ToActivity(profile, activity);
            response.Succeed = true;
            return response;
        }


    }
    public interface player_actionbuy : ISangoResponse
    {
        player_profile profile { get;set;}

        player_activity activity { get;set;}
    }
}
